package com.scoy.honeymei.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.scoy.honeymei.R;
import com.scoy.honeymei.bean.GoodsOrderBean;
import com.scoy.honeymei.utils.GlideImgUtil;
import com.scoy.honeymei.utils.MyUtil;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * @ClassName: NormalAdapter$
 * @Description:
 * @Author: GaoZhan
 * @CreateDate: 2019/8/26/0026$ 下午 17:03$
 * @UpdateUser: 更新者
 * @UpdateDate: 2019/8/26/0026$ 下午 17:03$
 * @UpdateRemark: 更新内容
 */
public class CartItemAdapter extends RecyclerView.Adapter {
    private Context context;
    private ArrayList<GoodsOrderBean> datalist;

    public CartItemAdapter(Context context, ArrayList<GoodsOrderBean> datalist) {
        this.context = context;
        this.datalist = datalist;
    }

    public interface OnOneClick {
        void oneClick(int pos);
    }

    OnOneClick onOneClick;

    public void setOnOneClick(OnOneClick onOneClick) {
        this.onOneClick = onOneClick;
    }

    public interface OnTwoClick {
        void twoClick(int pos);
    }

    OnTwoClick onTwoClick;

    public void setOnTwoClick(OnTwoClick onTwoClick) {
        this.onTwoClick = onTwoClick;
    }

    public interface OnThreeClick {
        void threeClick(int pos);
    }

    OnThreeClick onThreeClick;

    public void setOnThreeClick(OnThreeClick onThreeClick) {
        this.onThreeClick = onThreeClick;
    }

    public interface OnFourClick {
        void fourClick(int pos);
    }

    OnFourClick onFourClick;

    public void setOnFourClick(OnFourClick onFourClick) {
        this.onFourClick = onFourClick;
    }

    public interface OnFiveClick {
        void fiveClick(int pos);
    }

    OnFiveClick onFiveClick;

    public void setOnFiveClick(OnFiveClick onFiveClick) {
        this.onFiveClick = onFiveClick;
    }

    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(context).inflate(R.layout.item_cart_item, parent, false);
        return new CartItemHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position, @NonNull List payloads) {
        if (payloads.isEmpty()) {
            onBindViewHolder(holder, position);
        } else {
            CartItemHolder cartItemHolder = (CartItemHolder) holder;
            String loads = (String) payloads.get(0);
            if ("checkgoods".equals(loads)) {
                GlideImgUtil.glidePic(context, R.drawable.ic_checked_cart, cartItemHolder.cartCheckIv);
            } else if ("uncheckgoods".equals(loads)) {
                GlideImgUtil.glidePic(context, R.drawable.ic_uncheck_cart, cartItemHolder.cartCheckIv);
            }

        }

    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
        CartItemHolder cartItemHolder = (CartItemHolder) holder;
        GoodsOrderBean goodsOrderBean = datalist.get(position);
        GlideImgUtil.glidePic(context, goodsOrderBean.getImage(), cartItemHolder.cartImgIv);
        cartItemHolder.cartNameTv.setText(goodsOrderBean.getName());
        cartItemHolder.cartPriceTv.setText(goodsOrderBean.getMoney());
        cartItemHolder.ctNumTv.setText(goodsOrderBean.getNumber() + "");

        int goodsselect = goodsOrderBean.getGoodsselect();
        if (goodsselect == 1) {
            GlideImgUtil.glidePic(context, R.drawable.ic_checked_cart, cartItemHolder.cartCheckIv);
        } else {
            GlideImgUtil.glidePic(context, R.drawable.ic_uncheck_cart, cartItemHolder.cartCheckIv);
        }

        cartItemHolder.ctSubtractTv.setOnClickListener(new View.OnClickListener() {  //1 jian
            @Override
            public void onClick(View v) {
                if (onOneClick != null) {
                    String s0 = cartItemHolder.ctNumTv.getText().toString();
                    int i0 = Integer.parseInt(s0);
                    if (i0 <= 1) {
                        MyUtil.mytoast(context, "不能再少了");
                        return;
                    }
                    cartItemHolder.ctNumTv.setText((i0 - 1) + "");
                    goodsOrderBean.setNumber(i0 - 1);
                    onOneClick.oneClick(position);
                }
            }
        });

        cartItemHolder.ctAddTv.setOnClickListener(new View.OnClickListener() {  //2  add
            @Override
            public void onClick(View v) {
                if (onTwoClick != null) {
                    String s0 = cartItemHolder.ctNumTv.getText().toString();
                    int i0 = Integer.parseInt(s0);
                    cartItemHolder.ctNumTv.setText((i0 +1) + "");
                    goodsOrderBean.setNumber(i0 + 1);
                    onTwoClick.twoClick(position);
                }
            }
        });

        cartItemHolder.cartCheckIv.setOnClickListener(new View.OnClickListener() {  //3 select
            @Override
            public void onClick(View v) {
                if (onThreeClick != null) {
                    onThreeClick.threeClick(position);
                }
            }
        });

        cartItemHolder.delete.setOnClickListener(new View.OnClickListener() { //4 delete
            @Override
            public void onClick(View v) {
                if (onFourClick != null) {
                    onFourClick.fourClick(position);
                }
            }
        });

        cartItemHolder.itemView.setOnClickListener(new View.OnClickListener() {  //5 item
            @Override
            public void onClick(View v) {
                if (onFiveClick != null) {
                    onFiveClick.fiveClick(position);
                }
            }
        });

    }

    @Override
    public int getItemCount() {
        return datalist == null ? 0 : datalist.size();
    }


    class CartItemHolder extends RecyclerView.ViewHolder {
        @BindView(R.id.cart_check_iv)
        ImageView cartCheckIv;
        @BindView(R.id.cart_img_iv)
        ImageView cartImgIv;
        @BindView(R.id.cart_name_tv)
        TextView cartNameTv;
        @BindView(R.id.cart_pricesign_tv)
        TextView cartPricesignTv;
        @BindView(R.id.cart_price_tv)
        TextView cartPriceTv;
        @BindView(R.id.ct_subtract_tv)
        TextView ctSubtractTv;
        @BindView(R.id.ct_num_tv)
        TextView ctNumTv;
        @BindView(R.id.ct_add_tv)
        TextView ctAddTv;
        @BindView(R.id.cart_numcheck_tv)
        LinearLayout cartNumcheckTv;
        @BindView(R.id.delete)
        TextView delete;

        CartItemHolder(View view) {
            super(view);
            ButterKnife.bind(this, view);
        }
    }
}
